/**
 * @author banglong
 * @date 2016/9/26.
 */
define(['Layer', 'text!../html/components/bill/billDetailTemp.html!strip', 'avalon', 'getVmConfig', 'getApiConfig', 'tools', 'Condition'],
  function (layer, htmlTemp, avalon, getVmConfig, getApiConfig, tools, Condition) {

    return function () {

      var runtimeParam;
      var getRuntimeParam = function () {
        return {};
      };

      var param;
      var getVmOption = function () {
        return {
          $id: 'billDetail',
          billDetailList: [],
          columnNames: [],
          columnValueNames: [],

          isGYKS: false,
          BillType: '',
          bill: []
        }
      };

      this.init = function (param) {

        // if (!runtimeParam || !document.querySelector('#layui-layer' + runtimeParam.layeroObj)) {
        //     runtimeParam = getRuntimeParam();
        //
        //     runtimeParam.layeroObj = tools.createNewOpenWindow(['800px', ($(window).height() - 177 - 35) + 'px'], '查看明细', htmlTemp,
        //         null, {
        //             offset: ['178px', ($(window).width() - 800) + 'px'],
        //             shade: 0,
        //             vmConfig: getVmOption(),
        //             scrollbar: true,
        //             // 销毁方法
        //             destroy: function () {
        //                 runtimeParam = null;
        //                 billDetailVm = null;
        //             }
        //         });
        //
        //     billDetailVm = tools.initAvalonVm(avalon, getVmOption(), '.bill-detail-dialog');
        //     setTimeout(function () {
        //         $('#layui-layer' + runtimeParam.layeroObj).css({
        //             position: 'fixed',
        //             top: '178px'
        //         });
        //     }, 500);
        //
        //     billDetailVm.isGYKS = param.isGYKS;
        //     billDetailVm.BillType = param.BillType;
        //     billDetailVm.columnNames.pushArray(getVmConfig.getNewBillTableColumnNames(billDetailVm.isGYKS, billDetailVm.BillType)); // 获取表格列名
        //     billDetailVm.columnValueNames.pushArray(getVmConfig.getNewBillTableColumnValueNames(billDetailVm.isGYKS, billDetailVm.BillType)); // 获取表格列名
        // }
        //
        // runtimeParam.api = param.api;
        // billDetailVm.billDetailList.splice(0, billDetailVm.billDetailList.length);
        // billDetailVm.bill = JSON.parse(JSON.stringify(param.bill));

        var apiConfig = getApiConfig.buildNewBillRequestParam(param.isGYKS, false, 'view', param.BillType);
        // var layerIndex = layer.load();
        var requestParam;
        // 申请单需要特殊处理
        if (param.BillType === 'sq') {
          requestParam = {
            className: 'SqhzdMxManager',
            method: 'GetSqhzdMx',
            condition: JSON.stringify(new Condition("a.lsh", null, 'Equal', param.bill.BillSN, 'String'))
          };
        } else {
          requestParam = {
            className: apiConfig.getBillClassName,
            method: apiConfig.getBillMethod,
            billSN: param.bill.BillSN
          };
        }
        param.api.getBill(requestParam)
          .then(function (jsonResult) {

            if (jsonResult.issuccess !== 0 && jsonResult.result !== 'null') {
              return Promise.reject('查询信息失败！');
            }

            var result = JSON.parse(jsonResult.result).Details || JSON.parse(jsonResult.result);
            var CollectBills;
            if (param.BillType === 'dh' || param.BillType === 'jy') {
              CollectBills = JSON.parse(jsonResult.result).CollectBills;
            }
            if (param.BillType !== 'sq') {
              result.forEach(function (data) {
                data.consultMoney = data.RefPrice * parseInt(data.ApplyNum);
                data.Comment = data.Comment || '';
                if (param.BillType === 'dh' || param.BillType === 'jy') {
                  data.ApplyNum = data.Counts;
                  // 调换单的 收污数 修改
                  if (CollectBills) {
                    CollectBills.forEach(function (item) {
                      if (item.Details) {
                        var collectItem = item.Details.filter(function (item) {
                          return item.PackageID === data.PackageID;
                        });
                        collectItem.forEach(function (collect) {
                          data.ToCollectNum += collect.Count;
                        })
                      }
                    });
                  }
                }
              });
            }
            // param.billDetailList.pushArray(result);
            // layer.close(layerIndex);
            param.pushArray(result);

          })['catch'](function (ex) {
            // layer.close(layerIndex);
            tools.handleError(ex);
          });
      };
    };
  });